/**
 * @Function 调用远程服务
 * @ProjectName credit-data-platform
 * @PackageName com.py.credit.service.impl
 * @FileName RemoteBaseServiceImpl.java
 * @Date 2018年5月16日上午10:59:42
 * @Author Min DongXv
 * @Copyright (c) 2018, 金电联行 All Rights Reserved.
 */
package com.py.credit.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.constant.ApiPath;
import com.py.credit.constant.ApiPathUse;
import com.py.credit.entity.DictEntity;
import com.py.credit.entity.MsgContentEntity;
import com.py.credit.entity.SysUser;
import com.py.credit.helper.SendSysMsgHelper;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.entity.MenuEntity;
import com.py.framework.core.entity.SysAreaEntity;
import com.py.framework.core.entity.SysOfficeEntity;
import com.py.framework.core.entity.SysUserEntity;
import com.py.framework.core.entity.WorkFlowBus;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;

/**
 * @Function 调用远程服务
 * @ClassName RemoteBaseServiceImpl
 * @Date 2018年5月16日 上午10:59:42
 * @Author Min DongXv
 */
@Service
public class RemoteBaseServiceImpl implements RemoteBaseService {

    @Autowired
    private UrlHelper urlHelper;

    @Override
    public RestServiceResult<Integer> getAreaID(long deptId) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_AREA_BY_DEPTID;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("deptId", deptId + "");
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, "");
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<Integer>>() {
            });
        } catch (Exception e) {
            RestServiceResult<Integer> data = new RestServiceResult<Integer>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过用户ID获取用户列表调用异常！(RemoteBaseService#getAreaID) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    /**
     * @param regions
     * @return
     * @Function 通过区域ID获取区域名称对应MAP
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getAreaName
     * @Date 2018年5月16日 上午11:00:06
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<Map<String, String>> getAreaName(Set<String> regions, String jdToken) {
        Map<String, String> areaInfo = new HashMap<String, String>();
        RestServiceResult<Map<String, String>> areaResult = new RestServiceResult<Map<String, String>>();
        areaResult.setData(areaInfo);
        RestServiceResult<List<SysAreaEntity>> result = getAreaList(regions, jdToken);
        if (null != result && RestServiceResult.CODE_SUCCESS == result.getCode()
                && !CollectionUtils.isEmpty(result.getData())) {
            List<SysAreaEntity> areas = result.getData();
            for (SysAreaEntity area : areas) {
                if (null != area && StringUtils.isNotBlank(area.getAreaName())) {
                    areaInfo.put(String.valueOf(area.getId()), area.getAreaName());
                }
            }
        } else {
            areaResult.setCode(result.getCode());
            areaResult.setMsg(result.getMsg());
        }
        return areaResult;
    }

    /**
     * @param data     数据列表
     * @param result   结果集
     * @param token
     * @param regions  区域ID SET
     * @param idCode   列表中ID的key
     * @param nameCode 列表中name的key
     * @return
     * @Function 调用用户远程服务，进行区域名称填充
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName matchAreaName
     * @Date 2018年5月24日 下午4:10:53
     * @Author Min DongXv
     */
    @Override
    public List<Map<String, Object>> matchAreaName(List<Map<String, Object>> data,
                                                   RestServiceResult<List<Map<String, Object>>> result, String token, Set<String> regions, String idCode,
                                                   String nameCode) {
        // 请求基础服务获取区域名称
        if (!CollectionUtils.isEmpty(regions)) {
            RestServiceResult<Map<String, String>> areas = getAreaName(regions, token);
            if (RestServiceResult.CODE_SUCCESS != areas.getCode()) {
                result.setCode(areas.getCode());
                result.setMsg(areas.getMsg());
                return data;
            }
            Map<String, String> areaInfo = areas.getData();
            // 填充区域名称
            for (Iterator<Map<String, Object>> it = data.listIterator(); it.hasNext(); ) {
                Map<String, Object> map = it.next();
                if (null != map) {
                    String regionName = areaInfo.get(String.valueOf(map.get(idCode)));
                    if (StringUtils.isNotEmpty(regionName)) {
                        map.put(nameCode, regionName);
                    } else {
                        map.put(nameCode, "");
                    }
                }
            }
        }
        return data;
    }

    /**
     * @param regions
     * @return
     * @Function 通过区域ID获取区域列表
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getAreaList
     * @Date 2018年5月16日 上午11:00:06
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysAreaEntity>> getAreaList(Set<String> regions, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_REGION_NAME_BY_ID;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("ids", StringUtils.join(regions, ","));
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysAreaEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysAreaEntity>> areaData = new RestServiceResult<List<SysAreaEntity>>();
            areaData.setCode(RestServiceResult.CODE_UNKNOW);
            areaData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过区域ID获取区域列表调用异常！(RemoteBaseService#getAreaList) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return areaData;
        }
    }

    /**
     * @param list
     * @return
     * @Function 发送站内信
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName sendMsg
     * @Date 2018年5月16日 上午11:38:49
     * @Author Min DongXv
     */
    @Override
    public Future<HttpResponse> sendMsg(List<MsgContentEntity> list, String jdToken) {
        String params = JSON.toJSONString(list);
        Future<HttpResponse> response = null;
        try {
            response = SendSysMsgHelper.postSysMsg(jdToken, urlHelper.getBaseServerUrl(), params);
        } catch (Exception e) {
            ExceptionLogger.error("远程服务-发送站内信调用异常！(RemoteBaseService#sendMsg) Params:" + params, e.getMessage(), e);
            return null;
        }
        return response;
    }

    /**
     * @param ids 用户ID
     * @return
     * @Function 通过用户ID获取用户列表
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getUsersByIds
     * @Date 2018年5月19日 下午4:44:58
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysUserEntity>> getUsersByIds(Set<Integer> ids, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_USERS_BY_IDS;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("ids", StringUtils.join(ids, ","));
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysUserEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysUserEntity>> userData = new RestServiceResult<List<SysUserEntity>>();
            userData.setCode(RestServiceResult.CODE_UNKNOW);
            userData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过用户ID获取用户列表调用异常！(RemoteBaseService#getUsersByIds) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return userData;
        }
    }

    /**
     * @param ids
     * @return
     * @Function 获取用户id和名字对应关系
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getUserName
     * @Date 2018年5月22日 上午11:06:56
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<Map<String, String>> getUserName(Set<Integer> ids, String jdToken) {
        Map<String, String> userInfo = new HashMap<String, String>();
        RestServiceResult<Map<String, String>> userResult = new RestServiceResult<Map<String, String>>(userInfo);
        userResult.setData(userInfo);
        RestServiceResult<List<SysUserEntity>> users = getUsersByIds(ids, jdToken);
        if (null != userResult && RestServiceResult.CODE_SUCCESS == users.getCode()
                && !CollectionUtils.isEmpty(users.getData())) {
            for (SysUserEntity user : users.getData()) {
                if (null != user && StringUtils.isNotBlank(user.getRealName())) {
                    userInfo.put(String.valueOf(user.getId()), user.getRealName());
                }
            }
        } else {
            userResult.setCode(users.getCode());
            userResult.setMsg(users.getMsg());
        }
        return userResult;
    }

    /**
     * @param tableList 数据列表
     * @param result    返回结果集
     * @param token     token
     * @param userIds   用户ID SET
     * @param idCode    列表中ID的key
     * @param nameCode  列表中name的key
     * @return
     * @Function 调用用户远程服务，进行姓名填充
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName matchUserName
     * @Date 2018年5月9日 上午10:33:29
     * @Author Min DongXv
     */
    @Override
    public List<Map<String, Object>> matchUserName(List<Map<String, Object>> tableList,
                                                   RestServiceResult<List<Map<String, Object>>> result, String token, Set<Integer> userIds, String idCode,
                                                   String nameCode) {
        if (!CollectionUtils.isEmpty(userIds)) {
            // 通过用户ID获取用户名称
            RestServiceResult<Map<String, String>> userInfo = getUserName(userIds, token);
            if (RestServiceResult.CODE_SUCCESS != userInfo.getCode()) {
                result.setCode(userInfo.getCode());
                result.setMsg(userInfo.getMsg());
                return tableList;
            }
            Map<String, String> users = userInfo.getData();
            // 填充姓名信息
            for (Map<String, Object> map : tableList) {
                if (null != map) {
                    String userName = users.get(String.valueOf(map.get(idCode)));
                    if (StringUtils.isNotEmpty(userName)) {
                        map.put(nameCode, userName);
                    } else {
                        map.put(nameCode, "");
                    }
                }
            }
        }
        return tableList;
    }

    /**
     * @param tableList 数据列表
     * @param result    结果集
     * @param token
     * @param userIds   用户ID
     * @param idCode    列表中用户ID的key
     * @param nameCode  列表中用户名字的key
     * @param deptCode  列表中部门名字的key
     * @return
     * @Function 填充用户名字和部门名字
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName matchUserDeptName
     * @Date 2018年5月24日 下午4:59:33
     * @Author Min DongXv
     */
    @Override
    public List<Map<String, Object>> matchUserDeptName(List<Map<String, Object>> tableList,
                                                       RestServiceResult<List<Map<String, Object>>> result, String token, Set<Integer> userIds, String idCode,
                                                       String nameCode, String deptCode) {
        if (!CollectionUtils.isEmpty(userIds)) {
            // 通过用户ID获取用户名称
            RestServiceResult<List<SysUserEntity>> userInfo = getUsersByIds(userIds, token);
            if (RestServiceResult.CODE_SUCCESS != userInfo.getCode()) {
                result.setCode(userInfo.getCode());
                result.setMsg(userInfo.getMsg());
                return tableList;
            }
            List<SysUserEntity> users = userInfo.getData();
            Map<String, SysUserEntity> usersMap = new HashMap<String, SysUserEntity>(users.size());
            for (SysUserEntity entity : users) {
                if (null != entity && entity.getId() > 0) {
                    usersMap.put(String.valueOf(entity.getId()), entity);
                }
            }
            for (Map<String, Object> map : tableList) {
                if (null != map) {
                    SysUserEntity userEntity = usersMap.get(String.valueOf(map.get(idCode)));
                    if (null != userEntity) {
                        // 当自定义展示列有用户列时填充姓名信息
                        if (map.get(nameCode) != null) {
                            if (StringUtils.isNotEmpty(userEntity.getRealName())) {
                                map.put(nameCode, userEntity.getRealName());
                            } else {
                                map.put(nameCode, "");
                            }
                        }

                        // 当自定义展示列有部门列
                        // 填充部门信息
                        if (map.get(deptCode) != null) {
                            if (StringUtils.isNotEmpty(userEntity.getOfficeName())) {
                                map.put(deptCode, userEntity.getOfficeName());
                            } else {
                                map.put(deptCode, "");
                            }
                        }
                    }
                }
            }
        }
        return tableList;
    }

    /**
     * @return
     * @Function 获取当前用户信息
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getCurrentUser
     * @Date 2018年5月19日 下午4:44:59
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<SysUser> getCurrentUser(String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_CURRENT_USER;
        Map<String, String> params = new HashMap<String, String>(1);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<SysUser>>() {
            });
        } catch (Exception e) {
            RestServiceResult<SysUser> userData = new RestServiceResult<SysUser>();
            userData.setCode(RestServiceResult.CODE_UNKNOW);
            userData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取当前用户信息调用异常！(RemoteBaseService#getCurrentUser) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return userData;
        }
    }

    /**
     * @return
     * @Function 获取当前用户的ID
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getCurrentUserId
     * @Date 2018年5月21日 上午10:14:02
     * @Author Min DongXv
     */
    @Override
    public Long getCurrentUserId(String jdToken) {
        RestServiceResult<SysUser> user = getCurrentUser(jdToken);
        if (null != user && null != user.getData() && user.getData().getId() > 0) {
            return user.getData().getId();
        } else {
            return null;
        }
    }

    /**
     * @param areaId 区域ID为空获取所有部门信息
     * @return
     * @Function 通过区域ID获取部门信息
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getOfficesByAreaId
     * @Date 2018年5月21日 上午11:08:18
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysOfficeEntity>> getOfficesByAreaId(String areaId, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_OFFICES_BY_AREAID;
        // 区域ID为空获取所有的部门信息
        if (StringUtils.isEmpty(areaId)) {
            url = urlHelper.getBaseServerUrl() + ApiPath.GET_ALL_OFFICES;
        }
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("areaId", areaId);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysOfficeEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysOfficeEntity>> officeData = new RestServiceResult<List<SysOfficeEntity>>();
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取部门信息调用异常！(RemoteBaseService#getOfficesByAreaId) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    /**
     * @param jdToken
     * @return
     * @Function 获取当前用户的所有部门信息，包括下级子部门
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getCurrentUserOffices
     * @Date 2018年5月24日 上午10:55:44
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysOfficeEntity>> getCurrentUserOffices(String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_CURRENTUSER_OFFICES;
        Map<String, String> params = new HashMap<String, String>(1);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysOfficeEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysOfficeEntity>> officeData = new RestServiceResult<List<SysOfficeEntity>>();
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取当前用户的所有部门调用异常！(RemoteBaseService#getOfficesByCurrentUser) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    /**
     * @param jdToken
     * @return
     * @Function 获取当前用户的所有部门信息，包括下级子部门 ID和名字的Map
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getCurrentUserOfficesName
     * @Date 2018年5月24日 上午10:56:21
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<Map<String, String>> getCurrentUserOfficesName(String jdToken) {
        Map<String, String> deptInfo = new HashMap<String, String>();
        RestServiceResult<Map<String, String>> deptResult = new RestServiceResult<Map<String, String>>(deptInfo);
        RestServiceResult<List<SysOfficeEntity>> result = getCurrentUserOffices(jdToken);
        if (null != result && !CollectionUtils.isEmpty(result.getData())) {
            List<SysOfficeEntity> depts = result.getData();
            for (SysOfficeEntity dept : depts) {
                if (null != dept && StringUtils.isNotBlank(dept.getOfficeName())) {
                    deptInfo.put(String.valueOf(dept.getId()), dept.getOfficeName());
                }
            }
            deptResult.setCode(result.getCode());
            deptResult.setMsg(result.getMsg());
        }
        return deptResult;
    }

    @Override
    public RestServiceResult<List<MenuEntity>> getCurrentResourceMenus(String jdToken) {

        String url = urlHelper.getBaseServerUrl() + ApiPath.RESOURCE_GET_CURRENT_USER_RESOURCE;
        Map<String, String> params = new HashMap<String, String>(1);
        String result = "";
        RestServiceResult<List<MenuEntity>> officeData = new RestServiceResult<List<MenuEntity>>();
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<MenuEntity>>>() {
            });
        } catch (Exception e) {
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取获取当前用户的资源列表调用异常！(RemoteBaseService#getCurrentResourceMenus) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    /**
     * @param jdToken
     * @return
     * @Function 获取所有区域信息远程服务
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getAllAreas
     * @Date 2018年5月30日 下午1:49:06
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysAreaEntity>> getAllAreas(String areaId, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_ALL_AREAS;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("areaId", areaId);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysAreaEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysAreaEntity>> officeData = new RestServiceResult<List<SysAreaEntity>>();
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取所有区域信息调用异常！(RemoteBaseService#getAllAreas) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    /**
     * @param jdToken
     * @return
     * @Function 获取当前用户的区域及其下级区域信息
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getCurrentAreas
     * @Date 2018年6月1日 下午2:23:01
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysAreaEntity>> getCurrentAreas(String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_CURRENT_AREAS;
        Map<String, String> params = new HashMap<String, String>(1);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysAreaEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysAreaEntity>> officeData = new RestServiceResult<List<SysAreaEntity>>();
            officeData.setCode(RestServiceResult.CODE_UNKNOW);
            officeData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-获取当前用户区域信息调用异常！(RemoteBaseService#getCurrentAreas) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return officeData;
        }
    }

    /**
     * @param jdToken
     * @param dictKey 字典主表ID
     * @return
     * @Function 通过字典主表ID获取字典列表信息
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getDictByDictId
     * @Date 2018年6月26日 下午1:49:02
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<DictEntity>> getDictByDictKey(String dictKey) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DICT_DETAILS;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("dictKey", dictKey);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<DictEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<DictEntity>> data = new RestServiceResult<List<DictEntity>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典主表ID获取字典信息调用异常！(RemoteBaseService#getDictByDictKey) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    /**
     * @param jdToken
     * @param dictKey 字典主表key
     * @param dtlKey  字典值的key
     * @return
     * @Function 通过字典表ID获取特定字典表数据
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getDictByDtlKey
     * @Date 2018年6月26日 下午1:53:35
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<DictEntity> getDictByDtlKey(String dictKey, String dtlKey) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DICT_DETAIL;
        Map<String, String> params = new HashMap<String, String>(2);
        params.put("dictKey", dictKey);
        params.put("dtlKey", dtlKey);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<DictEntity>>() {
            });
        } catch (Exception e) {
            RestServiceResult<DictEntity> data = new RestServiceResult<DictEntity>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典表ID获取特定字典表数据调用异常！(RemoteBaseService#getDictByDtlKey) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    /**
     * @param jdToken
     * @param dictKey 字典主表key
     * @param dtlKey  字典值的key
     * @return
     * @Function 通过字典表ID获取特定字典表数据
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getDictByDtlKey
     * @Date 2018年6月26日 下午1:53:35
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<Map>> getLoginTimesByDept() {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_LOGIN_TIMES_BY_DEPT;
        Map<String, String> params = new HashMap<String, String>(2);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<Map>> data = new RestServiceResult<List<Map>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典表ID获取特定字典表数据调用异常！(RemoteBaseService#getDictByDtlKey) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<Map<String, Object>> addWorkFlow(String wfKey, String accessKey, String userId,
                                                              String sysId, String busId, String busName, String busUrl, String busType, String remark) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.WORKFLOW_ADD;
        Map<String, String> params = new HashMap<String, String>(12);
        params.put("wfKey", wfKey);
        params.put("accessKey", accessKey);
        params.put("userId", userId);
        params.put("sysId", sysId);
        params.put("busId", busId);
        params.put("busName", busName);
        params.put("busUrl", busUrl);
        params.put("busType", busType);
        params.put("remark", remark);
        String result = "";

        Map<String, String> map = new HashMap<String, String>();

        map.put("Content-Type", "application/json");

        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, JSON.toJSONString(params), map);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<Map<String, Object>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<Map<String, Object>> data = new RestServiceResult<Map<String, Object>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-添加工作流调用异常！(RemoteBaseService#addWorkFlow) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<List<WorkFlowBus>> getHandledWorkFlowBus() {
        Map<String, String> params = new HashMap<String, String>(1);
        String url = urlHelper.getBaseServerUrl() + ApiPath.WORKFLOW_HANDLED_ENTITY;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<WorkFlowBus>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<WorkFlowBus>> data = new RestServiceResult<List<WorkFlowBus>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-添加工作流调用异常！(RemoteBaseService#getHandledWorkFlowBus) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }

    }

    @Override
    public RestServiceResult<String> updateWorkFlowBusUpdateFlag(String id) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("id", id);
        String url = urlHelper.getBaseServerUrl() + ApiPath.WORKFLOW_UPDATE_ENTITY;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<String>>() {
            });
        } catch (Exception e) {
            RestServiceResult<String> data = new RestServiceResult<String>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-更新流程业务更新状态调用异常！(RemoteBaseService#updateWorkFlowBusUpdateFlag) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    /**
     * @param deptIds 部门ID，传空返回所有部门管理员
     * @param jdToken
     * @return
     * @Function 获取部门管理员数据
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getDeptAdminUsers
     * @Date 2018年7月26日 下午1:56:08
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<SysUser>> getDeptAdminUsers(String deptIds, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DEPTADMINS;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("deptIds", deptIds);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysUser>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysUser>> userData = new RestServiceResult<List<SysUser>>();
            userData.setCode(RestServiceResult.CODE_UNKNOW);
            userData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取部门管理员调用异常！(RemoteBaseService#getDeptAdminUsers) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return userData;
        }
    }

    @Override
    public RestServiceResult<Integer> getBusIdBuyTaskId(String taskId) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("taskId", taskId);
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_BUSID_BY_TASKID;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<Integer>>() {
            });
        } catch (Exception e) {
            RestServiceResult<Integer> data = new RestServiceResult<Integer>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-调用获取工作流异议处理业务id调用异常！(RemoteBaseService#getBusIdBuyTaskId) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<SysUserEntity> getLastAssignee(String procId) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("processId", procId);
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_LAST_ASSIGNEE;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<SysUserEntity>>() {
            });
        } catch (Exception e) {
            RestServiceResult<SysUserEntity> data = new RestServiceResult<SysUserEntity>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-最终审批人调用异常！(RemoteBaseService#getLastAssignee) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    /**
     * @param jdToken
     * @param dictKey 字典主表key
     * @param dtlKey  字典值的key
     * @return
     * @Function 通过字典表ID获取特定字典表数据
     * @FileName RemoteBaseServiceImpl.java
     * @MethodName getDictByDtlKey
     * @Date 2018年6月26日 下午1:53:35
     * @Author Min DongXv
     */
    @Override
    public RestServiceResult<List<Map>> getMessDatas() {
        String url = urlHelper.getBaseMessUrl() + ApiPath.GET_MESS_DATAS;
        Map<String, String> params = new HashMap<String, String>(2);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<Map>> data = new RestServiceResult<List<Map>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典表ID获取特定字典表数据调用异常！(RemoteBaseService#getDictByDtlKey) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<List<SysUserEntity>> getAllUserByTopDeptId(String deptId) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("deptId", deptId);
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DEPT_ALL_USERS;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysUserEntity>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<SysUserEntity>> data = new RestServiceResult<List<SysUserEntity>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-部门用户调用异常！(RemoteBaseService#getAllUserByTopDeptId) URL:" + url + " Params:"
                    + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<Integer> getDataCollectionMessageNum() {
        Map<String, String> params = new HashMap<String, String>(1);
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DATA_COLLECTION_MESSAGE_NUM;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<Integer>>() {
            });
        } catch (Exception e) {
            RestServiceResult<Integer> data = new RestServiceResult<Integer>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-催收信息数量调用异常！(RemoteBaseService#getDataCollectionMessageNum) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public List<LinkedHashMap> getManagerDeptList() {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_MANAGER_DEPT_LIST;
        Map<String, String> params = new HashMap<String, String>(1);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, "");
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<LinkedHashMap>>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<List<LinkedHashMap>> data = new RestServiceResult<List<LinkedHashMap>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取部门管理员的所有顶级部门id调用异常！(RemoteBaseService#getManagerDeptList) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }
    
    @Override
    public LinkedHashMap getUserInfo(int id) {
        String url = urlHelper.getBaseXypjUrl() + ApiPath.GET_USER_INFO;
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("id", String.valueOf(id));
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, "");
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<LinkedHashMap>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<LinkedHashMap> data = new RestServiceResult<LinkedHashMap>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取部门管理员的所有顶级部门id调用异常！(RemoteBaseService#getManagerDeptList) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }
    
    @Override
    public String updateCheckReport(int id,String checkTime,String checkStatus,String pdfpath) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("id", String.valueOf(id));
        params.put("checkTime", checkTime);
        params.put("checkStatus", checkStatus);
        params.put("pdfpath", pdfpath);
        String url = urlHelper.getBaseXypjUrl() + ApiPath.UPDATE_CHECK_STATUS;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<String>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<String> data = new RestServiceResult<String>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-更新流程业务更新状态调用异常！(RemoteBaseService#updateWorkFlowBusUpdateFlag) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data.getData();
        }
    }

    @Override
    public LinkedHashMap getDissentProcessHistory(String key, String id) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_DISSENT_PROCESS_HISTORY;
        Map<String, String> params = new HashMap<String, String>(2);
        params.put("key", key);
        params.put("id", id);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, "");
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<LinkedHashMap>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<LinkedHashMap> data = new RestServiceResult<>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取异议处理历史记录异常！(RemoteBaseService#getDissentProcessHistory) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }

    @Override
    public List<LinkedHashMap> examineTask(
            String taskId, String procId, String userId, String paramName,
            String paramValue, String comment, SysUser user, String jdToken) {
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_EXAMINE_TASK;
        Map<String, String> params = new HashMap<>(8);
        params.put("taskId", taskId);
        params.put("procId", procId);
        params.put("userId", userId);
        params.put("paramName", paramName);
        params.put("paramValue", paramValue);
        params.put("comment", comment);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<LinkedHashMap>>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<List<LinkedHashMap>> data = new RestServiceResult<List<LinkedHashMap>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-异议处理调用流程审核异常！(RemoteBaseService#examineTask) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
            return data.getData();
        }
    }
    
    
    @Override
    public RestServiceResult<List<Map>> getlhcf(String ztmc,String bm,String type, String jdToken) {
        String url = urlHelper.getBaseMessUrl() + "/integratedQuery/guiji.json";
        Map<String, String> params = new HashMap<String, String>(3);
        params.put("ztmc", ztmc);
        params.put("bm", bm);
        params.put("type", type);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<Map>> data = new RestServiceResult<List<Map>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典表ID获取特定字典表数据调用异常！(RemoteBaseService#getlhcf) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }
    
    @Override
    public RestServiceResult<List<Map>> getXyjg(String ztmc,String bm,String type, String jdToken) {
        String url = urlHelper.getBaseXypjUrl() + "/szjg/guiji.json";
        Map<String, String> params = new HashMap<String, String>(3);
        params.put("ztmc", ztmc);
        params.put("bm", bm);
        params.put("type", type);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, jdToken);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map>>>() {
            });
        } catch (Exception e) {
            RestServiceResult<List<Map>> data = new RestServiceResult<List<Map>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-通过字典表ID获取特定字典表数据调用异常！(RemoteBaseService#getlhcf) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }
    
    @Override
    public List<Map<String, Object>>  getRuleData(String tybm,String kId) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("tybm", tybm);
        params.put("kId", kId);
        String url = urlHelper.getBaseXypjUrl() + ApiPathUse.GET_RULE_DATA;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map<String, Object>>>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<List<Map<String, Object>> > data = new RestServiceResult<List<Map<String, Object>> >();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-家政服务获取信用评价得分、等级接口调用异常！(RemoteBaseService#getRuleData.json) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data.getData();
        }
    }
    
    @Override
    public List<Map<String, String>>   getIndexInfo(String data1) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("data", data1);
        String url = urlHelper.getBaseXypjUrl() + ApiPathUse.GET_INDEX_DATA;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<Map<String, String>>>>() {
            }).getData();
        } catch (Exception e) {
            RestServiceResult<List<Map<String, String>>> data = new RestServiceResult<List<Map<String, String>>>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-家政服务获取指标信息接口调用异常！(RemoteBaseService#getIndexInfo.json) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data.getData();
        }
    }

	@Override
	public RestServiceResult<List<SysUser>> getUsers() {
		  String url = urlHelper.getBaseServerUrl() + ApiPath.GET_ALL_USERS;
	       
	        Map<String, String> params = new HashMap<String, String>(1);
	       
	        String result = "";
	        try {
	            // 发送请求
	            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
	            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<List<SysUser>>>() {
	            });
	        } catch (Exception e) {
	            RestServiceResult<List<SysUser>> userData = new RestServiceResult<List<SysUser>>();
	            userData.setCode(RestServiceResult.CODE_UNKNOW);
	            userData.setMsg("系统异常，请稍后再试！");
	            ExceptionLogger.error("远程服务-获取用户信息调用异常！(RemoteBaseService#getUsers) URL:" + url + " Params:"
	                    + params.toString() + "Result:" + result, e.getMessage(), e);
	            return userData;
	        }
	}
}
